/**
 *  使用优惠劵
 */
define(function(require,exports,module) {
 //    require('./plug/hammer.min');
	// require('./plug/layer/layer');
    var myPub = require('./public');
    var myGift = require('./giftCard');

    var oShadow = $('.js_shadow');				        //阴影
    var oShadow1 = $('.js_shadow1');				    //阴影1
    var oCouponSeletSide = $('.js_couponSeletSide');    //优惠劵领取弹框 (公用框)
    var oUseCoupon = $('.js_useCoupon');                //优惠劵列表展示按钮-商品详情页面
    var oGoodsCoupon = $('.js_goodsCoupon');            //详情优惠劵框
    var oUseCouponSubOrd = $('.js_useCouponSubOrd');    //优惠劵列表展示按钮-提交订单页面
    var oUseCouponBusiness = $('.js_useCouponBusiness');    // 商家优惠操作入口

    var oSubOrdCoupon = $('.js_subOrdCoupon');              // 提交订单 优惠劵框 (单独用)
    var oSubOrdCouponBus = $('.js_subOrdCouponBus');        // 提交订单 商家优惠框 (单独用)
    var oCoupon_business = $('.js_coupon_business');        //商家优惠劵


    var moveHeight = oCouponSeletSide.outerHeight(true);    //物品详情优惠劵高度
    oCouponSeletSide.css({'bottom': -moveHeight });

    var moveWidth = oCoupon_business.find('li').outerWidth(true); //商家优惠劵单个宽度
    oCoupon_business.find('ul').css({ 'width' : moveWidth * oCoupon_business.find('li').length});
    var oCloseBtn = $('.js_closeBtn');
    var bSideOff = true;

    // 物品详情优惠劵领取
    oUseCoupon.hammer().on('tap', function () {
        if(!bSideOff)  return;
        bSideOff = false;
        myPub.OffMuiEvent(false);
        oCouponSeletSide.show().stop().animate({ 'bottom':0 },300,function() {
            oShadow.show();
            $('html').addClass('bodyHeight100');
            bSideOff = true
        });
        myPub.HamstopPropaga();
    });

    // 物品详情专用 内部mui控件
    oShadow.hammer().on('tap', function () {
        //隐藏服务
        oCouponSeletSide.stop().animate({bottom:-moveHeight},300,function(){
            oCouponSeletSide.hide();
            oShadow.hide();
            $('html').removeClass('bodyHeight100');
            bSideOff = true
        });
        myPub.HamstopPropaga();
        myPub.OffMuiEvent(true);
    });

    // 其他地方可以公用
    oShadow1.hammer().on('tap', function () {
        //隐藏服务
        oCouponSeletSide.stop().animate({bottom:-moveHeight},300,function(){
            oCouponSeletSide.hide();
            oShadow1.hide();
            $('html').removeClass('bodyHeight100');
            bSideOff = true
        });
        myPub.HamstopPropaga();
    });

    oCloseBtn.hammer().on('tap', function () {
        //隐藏服务
        oCouponSeletSide.stop().animate({bottom:-moveHeight},300,function(){
            oCouponSeletSide.hide();
            oShadow.hide();
            $('html').removeClass('bodyHeight100');
            bSideOff = true
        });
        myPub.HamstopPropaga();
    });

    // 立即领取操作
    oGoodsCoupon.hammer().on('tap', function (e) {
        var _tarTag = e.gesture.target;
        var target;
        if(_tarTag.nodeName.toLowerCase() == 'button') {
            target = $(_tarTag);      //target 是当前 点击的 领取按钮
            myPub.TipLayer('领取成功！');
        }

        myPub.HamstopPropaga();
    });


    // 商家优惠劵领取
    oCoupon_business.find('li').on('click', function () {
        myPub.TipLayer('领取成功！');
        myPub.HamstopPropaga();
    });


    // 提交订单选 优惠劵
    oUseCouponSubOrd.hammer().on('tap', function () {
        if ($(this).find('.couponOff').length > 0){
            return;
        }
        if(!bSideOff)  return;
        bSideOff = false;
        oSubOrdCoupon.show().stop().animate({ 'bottom':0 },300,function() {
            oShadow1.show();
            $('html').addClass('bodyHeight100');
            bSideOff = true;
        });
        myPub.HamstopPropaga();
    });

    // 不使用优惠劵
    $('.js_couponBtn_close').hammer().on('tap', function () {


        // 隐藏弹框
        oSubOrdCoupon.stop().animate({bottom:-moveHeight},300,function(){
            oSubOrdCoupon.hide();
            oShadow1.hide();
            $('html').removeClass('bodyHeight100');
            bSideOff = true
        });
    });


    //平台优惠劵使用选择
    oSubOrdCoupon.find('.js_coupon').hammer().on('tap', function () {
        if($(this).hasClass('on')) {
            oSubOrdCoupon.find('.js_coupon').removeClass('active_couponSeleted on');
            $('.couponDiscountPrice').text('0.0');
            $('.couponDiscount').hide();
            $('.couponOn').show();
        }
        else {
            oSubOrdCoupon.find('.js_coupon').removeClass('active_couponSeleted on');
            $(this).addClass('active_couponSeleted on');
            var couponPrice = $(this).find('.couponCostprice').text();
            $('.couponDiscountPrice').text(couponPrice);
            $('.couponDiscount').show();
            $('.couponOn').hide();
        }
        myGift.calcLastPrice();
        myPub.HamstopPropaga();
    });


    // 商家优惠使用
    oUseCouponBusiness.hammer().on('tap', function () {
        if ($(this).find('.couponOff').length > 0){
            return;
        }
        if(!bSideOff)  return;
        bSideOff = false;

        $(this).siblings('.js_subOrdCouponBus').show().stop().animate({ 'bottom':0 },300,function() {
            oShadow1.show();
            $('html').addClass('bodyHeight100');
            bSideOff = true;
        });
        myPub.HamstopPropaga();
    });


    //商家优惠点击选择
    oSubOrdCouponBus.find('li').hammer().on('tap', function () {
        $(this).parents('.js_subOrdCouponBus').find('li').removeClass('active on');
        if($(this).hasClass('on')) {
            $(this).removeClass('active on');
        } else {
            $(this).addClass('active on');
        }
        // 获取选择的信息
        var _getInfo = $(this).find('span').eq(0).text();

        //设置页面 商家优惠信息
        $(this).parents('.js_subOrdCouponBus').siblings('.js_useCouponBusiness').find('.js_businessPay').text(_getInfo);

        // 隐藏商家优惠弹框
        oSubOrdCouponBus.stop().animate({bottom:-moveHeight},300,function(){
            oSubOrdCouponBus.hide();
            oShadow1.hide();
            $('html').removeClass('bodyHeight100');
            bSideOff = true;
        });

        myPub.HamstopPropaga();
        myGift.calcLastPrice();

        var couponId = $(this).attr("couponId");

        console.log("aaaa --- bbbb");

        console.log('couponLimit[couponId]  ' + couponLimit[couponId]);
    });

    oSubOrdCouponBus.find('.js_closeBtn').hammer().on('tap', function () {
        oSubOrdCouponBus.stop().animate({bottom:-moveHeight},300,function(){
            oSubOrdCouponBus.hide();
            oShadow1.hide();
            $('html').removeClass('bodyHeight100');
            bSideOff = true;
        });
    });


});